import { Inject, Injectable } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { AuthStrategy } from '@/constants/auth.constant'
import { IJwtConfig, JwtConfig } from '@/config/index'

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, AuthStrategy.JWT) {
	constructor(@Inject(JwtConfig.KEY) private JwtConfig: IJwtConfig) {
		super({
			jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
			ignoreExpiration: false,
			secretOrKey: JwtConfig.secretKey,
		})
	}
	/** token解析成功后 会将返回值 构建一个 user 对象，并将其作为属性附加到 Request 对象上 */
	async validate(payload) {
		// payload: JwtService.sign 的参数
		return payload
	}
}
